<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@page import="java.sql.*"%>
<%@page import="com.mysql.*"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.*" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!doctype html>
<html lang="en">
<head>
 
  <title>jQuery UI Droppable - Simple photo manager</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
   <link rel="stylesheet" href="resources/mytheme/css/style.css">
  <style>
  #gallery { float: left; width: 65%; min-height: 12em; }
  .gallery.custom-state-active { background: #eee; }
  .gallery li { float: left; width: 96px; padding: 0.4em; margin: 0 0.4em 0.4em 0; text-align: center; }
  .gallery li h5 { margin: 0 0 0.4em; cursor: move; }
  .gallery li a { float: right; }
  .gallery li a.ui-icon-zoomin { float: left; }
  .gallery li img { width: 100%; cursor: move; }
 
  #trash { float: right; width: 32%; min-height: 18em; padding: 1%; }
  #trash h4 { line-height: 16px; margin: 0 0 0.4em; }
  #trash h4 .ui-icon { float: left; }
  #trash .gallery h5 { display: none; }
  </style>
  <script>
  $(function() {
    // there's the gallery and the trash
    var $gallery = $( "#gallery" ),
      $trash = $( "#trash" );
 
    // let the gallery items be draggable
    $( "li", $gallery ).draggable({
      cancel: "a.ui-icon", // clicking an icon won't initiate dragging
      revert: "invalid", // when not dropped, the item will revert back to its initial position
      containment: "document",
      helper: "clone",
      cursor: "move"
    });
 
    // let the trash be droppable, accepting the gallery items
    $trash.droppable({
      accept: "#gallery > li",
      activeClass: "ui-state-highlight",
      drop: function( event, ui ) {
        deleteImage( ui.draggable );
      }
    });
 
    // let the gallery be droppable as well, accepting items from the trash
    $gallery.droppable({
      accept: "#trash li",
      activeClass: "custom-state-active",
      drop: function( event, ui ) {
        recycleImage( ui.draggable );
      }
    });
 
    // image deletion function
    var recycle_icon = "<a href='link/to/recycle/script/when/we/have/js/off' title='Recycle this image' class='ui-icon ui-icon-trash'>Recycle image</a>";
    function deleteImage( $item ) {
      $item.fadeOut(function() {
        var $list = $( "ul", $trash ).length ?
          $( "ul", $trash ) :
          $( "<ul class='gallery ui-helper-reset'/>" ).appendTo( $trash );
 
        $item.find( "a.ui-icon-cart" ).remove();
        $item.append( recycle_icon ).appendTo( $list ).fadeIn(function() {
          $item
            .animate({ width: "48px" })
            .find( "img" )
              .animate({ height: "72px" });
        });
      });
    }
 
    // image recycle function
    var trash_icon = "<a href='link/to/trash/script/when/we/have/js/off' title='Delete this image' class='ui-icon ui-icon-cart'>Delete image</a>";
    function recycleImage( $item ) {
      $item.fadeOut(function() {
        $item
          .find( "a.ui-icon-trash" )
            .remove()
          .end()
          .css( "width", "100px")
          .append( trash_icon )
          .find( "img" )
            .css( "height", "130px" )
          .end()
          .appendTo( $gallery )
          .fadeIn();
      });
    }

    function checkout( $item ) {
        $item.fadeOut(function() {
          $item
            .find( "a.ui-icon-trash" )
              .remove()
            .end();
        });
    }
    // image preview function, demonstrating the ui.dialog used as a modal window
    function viewLargerImage( $link ) {
      var src = $link.attr( "href" ),
        title = $link.siblings( "img" ).attr( "alt" ),
        $modal = $( "img[src$='" + src + "']" );
 
      if ( $modal.length ) {
        $modal.dialog( "open" );
      } else {
        var img = $( "<img alt='" + title + "' width='384' height='288' style='display: none; padding: 8px;' />" )
          .attr( "src", src ).appendTo( "body" );
        setTimeout(function() {
          img.dialog({
            title: title,
            width: 400,
            modal: true
          });
        }, 1 );
      }
    }
 
    // resolve the icons behavior with event delegation
    $( "ul.gallery > li" ).click(function( event ) {
      var $item = $( this ),
        $target = $( event.target );
 
      if ( $target.is( "a.ui-icon-cart" ) ) {
        deleteImage( $item );
      } else if ( $target.is( "a.ui-icon-trash" ) ) {
        recycleImage( $item );
      }
 
      return false;
    });
  });

  function checkOut(){
	  var list = new Array();
	  $( "#trash li" ).each(function( index ) {
		  //alert("item in");
		  list.push(this.id);
		  number = this.id;
		});



		//var otherlist = ["1","4","7","8"];

	  $.ajax({
          type:"POST",
          url:"cart",
          data: { "queueIDs":list } ,
          sucess:function(data){
              alert("AJAX sucess");
              window.location.href = "MovieProject/viewQueue";
          }
      });
	  
	  $( document ).ajaxComplete(function() {

                  window.location.href = "viewqueue";

                });





	  }

  </script>
</head>
<body>
 
 
 <%
    try{
    	int u = (Integer)request.getSession().getAttribute("uName");
    	
Connection connection = 
         DriverManager.getConnection
            ("jdbc:mysql://localhost:3306/moviedatabase","dbuser","dbpassword");
       Statement statement = connection.createStatement() ;
     	ResultSet rs = statement.executeQuery("select * from movies JOIN queue ON movies.MovieID = queue.MovieID JOIN users ON users._z4 = queue.UserID WHERE queue.UserID ="+u+";") ;  
     
%>
<div id=header2>

	<div id="logo">
		<img src="resources/mytheme/Logo.png" alt="RentFlix">
	</div>
	
	<div id="searchNav">
		<form name="inputForm" method="get">
			<input class="searchBox" type="text" name="searchBox" placeholder="Search for Movies..." onkeyup="fastSearch()">
			<input class="searchButton" type="submit" value="Search">
			<br>
			<!-- <input class="checkbox" type="checkbox" name="desc" value="show">Show Description -->
		</form>
	</div>
	
	<div id="navBar">
		<ul>
		    <li><a href="/MovieProject/browse">Movies</a></li>
		    <li><a href="/MovieProject/search?rating=g">Kids</a></li>
		    <li><a href="/MovieProject/viewqueue">Queue</a></li>
		</ul>
	</div>
	
	<div id="loginBar">
		<ul>
<!-- 			<li><a class="lastlink" href="http://localhost:8080/MovieProject">Sign Out</a> </li> -->

			<li><div id="signoutform"><form action="signout" method="post" name="loutform"><input class="lastlink" type="submit" value="Sign Out" style="text-transform:capitalize;"></form></div></li>
			
			<li><a class="firstlink" href="http://localhost:8080/MovieProject/search">Home</a> </li>
<%-- 			<li><%=request.getAttribute("message")%></li> --%>
				
			
			
				
			
		</ul>
	</div>
	
</div>
<div id="cartbody"> 
<div class="ui-widget ui-helper-clearfix">

<ul id="gallery" class="gallery ui-helper-reset ui-helper-clearfix">

<%   
			while(rs.next()){  %>
			
			<li class="ui-widget-content ui-corner-tr" id="<%= rs.getString(9)%>">
			<input type="hidden" name="id" id="id" value="<%= rs.getString(9)%>" />
			    <h5 class="ui-widget-header"><%= rs.getString(2)%></h5>
			    <img src="<%= rs.getString(8)%>" alt="" width="50" height="130">
			    <a href="link/to/trash/script/when/we/have/js/off" title="Check Out" class="ui-icon ui-icon-cart">Check out</a>
			</li>
				
				
		<% } %>


</ul>

<div id="trash" class="ui-widget-content ui-state-default">
  <h4 class="ui-widget-header"><span class="ui-icon ui-icon-cart">Cart</span> Cart</h4>
  </div>


  <div>
  
  <input type="submit" value="Check Out" onclick="checkOut()">
  
</div>

</div>
</div>

 <%
        }
        catch(Exception e)
        {
             out.println("wrong entry"+e);
        }
%>
 <div id="footer">
	<div id="foota">
		<p>Get Help</p>
		<ul>
		  <li><a href="#">FAQ</a></li>
		  <li><a href="#">Help Center</a></li>
		  <li><a href="#">Account</a></li>
		  <li><a href="#">Questions</a></li>
		</ul>
	</div>

	<div id="footb"><p>Our Company</p>
	
		<ul>
		  <li><a href="#">About us </a></li>
		  <li><a href="#">Reviews  </a></li>
		  <li><a href="#">Jobs  </a></li>
		  <li><a href="#">Terms of Use</a></li>
		</ul>
	</div>
	
	<div id="footc"><p>Getting Started</p>
		<ul>
		  <li><a href="#">Free Trail Offer Details  </a></li>
		  <li><a href="#"> Redeem Your Gift</a></li>
		</ul>
	</div>
</div>
</body>
</html>